namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorVolumeRatio : BaseIndicator
    {
        private LineShape tlsMVr;
        private LineShape tlsVr;

        public IndicatorVolumeRatio()
        {
            base.IndType = IndicatorType.VR;
            base.IndName = "VR";
            base.IndDesc = "容积比率";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 26.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "N1", 6.0));
            base.SourceDic["Close"] = "";
            base.SourceDic["Volume"] = "";
            base.fields = new string[] { "VR", "MVR" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.tlsVr) && (obj != this.tlsMVr))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            int num = r - (((int) base.IndPeriod[0].ParamValue) - 1);
            if (num < 0)
            {
                num = 0;
            }
            double num2 = 0.0;
            double num3 = 0.0;
            double num4 = 0.0;
            double curValue = 0.0;
            if (r >= (((int) base.IndPeriod[0].ParamValue) - 1))
            {
                for (int i = r; i >= num; i--)
                {
                    double num7 = Convert.ToDouble(base.Crab.DataSource.Rows[i][base.SourceDic["Close"]]);
                    double num8 = 0.0;
                    if (i > 0)
                    {
                        num8 = Convert.ToDouble(base.Crab.DataSource.Rows[i - 1][base.SourceDic["Close"]]);
                    }
                    double num9 = Convert.ToDouble(base.Crab.DataSource.Rows[i][base.SourceDic["Volume"]]);
                    if (num7 > num8)
                    {
                        num2 += num9;
                    }
                    else if (num7 < num8)
                    {
                        num3 += num9;
                    }
                    else
                    {
                        num4 += num9;
                    }
                }
                if ((num3 + (num4 / 2.0)) != 0.0)
                {
                    curValue = ((num2 + (num4 / 2.0)) / (num3 + (num4 / 2.0))) * 100.0;
                }
            }
            double num10 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[1].ParamValue, this.tlsMVr.Name, this.tlsVr.Name, curValue, base.Crab.DataSource);
            return new double[] { curValue, num10 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsVr, this.tlsMVr };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsVr);
            base.Crab.DeleteShape(this.TlsMVr);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.TlsVr.Name, numArray[0], r);
                        base.Crab.SetValue(this.TlsMVr.Name, numArray[1], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsVr = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsMVr = base.Crab.AddLine(fields[1], panelID);
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            base.SourceDic["Close"] = source[0];
            base.SourceDic["Volume"] = source[1];
        }

        public LineShape TlsMVr
        {
            get
            {
                return this.tlsMVr;
            }
            set
            {
                this.tlsMVr = value;
            }
        }

        public LineShape TlsVr
        {
            get
            {
                return this.tlsVr;
            }
            set
            {
                this.tlsVr = value;
            }
        }
    }
}

